// 收货地址

<template>
  <view class="page" :class="pageClass">
    <!-- 地址 -->
    <view class="address-box">
      <view class="address-item" v-for="item in addressList" :key="item.id">
        <view class="item" @tap="chooseAddress(item)">
          <view class="item-tag">
            <view v-if="item.is_default == 1">默认</view>
          </view>
          <view class="item-info">
            <view class="info-title">{{
              item.province_name +
              item.city_name +
              item.area_name +
              item.detail_address
            }}</view>
            <view class="info-user">
              <text>{{ item.connect_name }}</text>
              <text>{{ item.connect_mobile }}</text>
            </view>
          </view>
          <view class="item-edit">
            <view @tap.stop="editAddress(item)">编辑</view>
          </view>
        </view>
      </view>
      <dht-empty type="no_address" v-if="noData"></dht-empty>
    </view>
    <!-- 按钮 -->
    <view class="add-box">
      <view class="btn-box" @tap="addAddress">+ 新建收货地址</view>
    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      pageClass: 'page-box',
      // 空白页
      noData: false,
      type: '',
      addressList: []
    }
  },
  onLoad(options) {
    this.type = options.type
  },
  onShow() {
    this.getAddress()
  },
  methods: {
    // 获取收货地址
    async getAddress() {
      const res = await this.$http.post('/api/kuerp/order/address/getlist', {
        page: -1
      })
      if (res.code == 200) {
        this.addressList = res.data.list
        this.noData = !this.addressList.length
        if (this.noData) {
          this.pageClass = 'no-background'
        } else {
          this.pageClass = 'page-box'
        }
      }
    },
    // 编辑收货地址
    editAddress(item) {
      console.log(item)
      uni.navigateTo({
        url: `/pages/my/commonFunction/address/add?type=edit&address=${JSON.stringify(
          item
        )}`
      })
    },
    // 新建收货地址
    addAddress() {
      uni.navigateTo({ url: '/pages/my/commonFunction/address/add?type=add' })
    },
    // 使用收货地址
    chooseAddress(item) {
      if (this.type === 'choose') {
        uni.$emit('chooseAddress', { item })
        uni.navigateBack({ delta: 1 })
      } else if (this.type === 'submitOrder') {
        const pages = getCurrentPages()
        const prepage = pages[pages.length - 2]
        prepage.$vm.postForm.address_id = item.id
        prepage.$vm.addressDetail = item
        if (item.connect_mobile.length == 11) {
          let connect_mobile = item.connect_mobile.split('')
          connect_mobile.splice(3, 4, '****')
          prepage.$vm.addressDetail.connect_mobile = connect_mobile.join('')
        }
        uni.navigateBack({ delta: 1 })
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.page {
  min-height: 100vh;
  overflow: hidden;
}
.page-box {
  background-color: #f7f7f7;
}
.no-background {
  background-color: #fff;
}
// 地址
.address-box {
  overflow: hidden;
  background-color: #fff;
  .address-item {
    display: flex;
    align-items: center;
    margin: 0 auto;
    width: 702rpx;
    height: 220rpx;
    border-bottom: 1px solid #ddd;
    &:last-child {
      border-bottom: none;
    }
    .item {
      display: flex;
      flex: 1;
      .item-tag {
        width: 80rpx;
        margin-top: 8rpx;
        view {
          display: flex;
          justify-content: center;
          align-items: center;
          width: 64rpx;
          height: 36rpx;
          background: #fe492f;
          border-radius: 4rpx;
          font-size: 24rpx;
          color: #ffffff;
        }
      }
      .item-info {
        flex: 1;
        .info-title {
          font-size: 32rpx;
          color: #111111;
          line-height: 48rpx;
          overflow: hidden;
          text-overflow: ellipsis;
          display: -webkit-box;
          -webkit-line-clamp: 2;
          -webkit-box-orient: vertical;
        }
        .info-user {
          margin-top: 8rpx;
          text {
            font-size: 24rpx;
            color: #111111;
            line-height: 36rpx;
            &:first-child {
              margin-right: 20rpx;
            }
          }
        }
      }
      .item-edit {
        display: flex;
        align-items: flex-end;
        view {
          display: flex;
          justify-content: center;
          align-items: center;
          margin-bottom: -16rpx;
          width: 120rpx;
          height: 56rpx;
          border-radius: 30rpx;
          border: 1px solid #cccccc;
        }
      }
    }
  }
}

// 按钮
.add-box {
  width: 100%;
  position: fixed;
  bottom: 90rpx;
  display: flex;
  justify-content: center;
  align-items: center;
  left: 0;
  .btn-box {
    display: flex;
    justify-content: center;
    align-items: center;

    width: 702rpx;
    height: 92rpx;
    background: #fe492f;
    border-radius: 50rpx;
    font-size: 36rpx;
    font-weight: 500;
    color: #ffffff;
  }
}
</style>
